package com.o.mj.controller;

import com.o.mj.pojo.Company;
import com.o.mj.pojo.DataColleges;
import com.o.mj.pojo.UserInfo;
import com.o.mj.service.PersonService;
import com.o.mj.service.WorkPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 首页控制
 */

@Controller
public class HomePageController {

    @Autowired
    WorkPersonService workPS;

    @Autowired
    PersonService personS;

    /**
     * 首页
     */
    @RequestMapping("/homePage")
    public String homePage(Model model, HttpSession session) {
        try {
            try {
                Object userId = session.getAttribute("userId");
                String srcBase = personS.getImgFileToBase64((String) userId);
                model.addAttribute("srcBase", srcBase);
                UserInfo usi = workPS.getPersonMessage((String) userId);
                model.addAttribute("aiName", usi.getAiName());
            } catch (Exception e) {

            }
            //就业人数
            String worker = workPS.workPerson();
            model.addAttribute("workers", worker);

            //学生总数
            String person = workPS.allPerson();
            model.addAttribute("persons", person);

            //今日就业人数
            String today = workPS.todayWorked();
            model.addAttribute("todayS", today);

            //考公读研人数
            String number = workPS.workBook();
            model.addAttribute("numbers", number);

            //公司数量
            List<Company> company = workPS.getCompany();
            model.addAttribute("Company", company);

            // 创建存储头像的新文件夹
            String filePath = "c://Ming";
            File file = new File(filePath);
            if (!file.exists()) {
                file.mkdirs();
            }
        } catch (Exception e) {
        }
        return "index";
    }

    /**
     * 两份统计图数据
     */
    @RequestMapping("/chart")
    @ResponseBody
    public List<DataColleges> chart() {
        List<DataColleges> list = new ArrayList<>();
        List college = workPS.Colleges();
        List dataC = workPS.getDate();
        list.addAll(college);
        list.addAll(dataC);
        return list;
    }


}
